$(function () {
    vm.bindType();
    vm.getDeptId();
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            startNo:null,
            endNo:null,
            name:null,
            phone:null,
            agentId:null,
            isOnline:true,
            amount:null,
            isActiver:true,
            terminalType:0,
            activDueTime:null,
            dueAmount:null
        },
        isReport:false,
        isReback:false,
        showReBack: true,
        showDistribu: false,
        flag:false,
        showAmount:true,
        terminalTypes:[],
        deptId:null
    },
    methods: {
        initQ:function(){
            vm.q={
                startNo:null,
                endNo:null,
                name:null,
                phone:null,
                agentId:null,
                isOnline:true,
                amount:null,
                isActiver:true,
                terminalType:0,
                activDueTime:null,
                dueAmount:null
            }
        },
        //回拨终端
        rebackTer: function () {
            vm.initQ();
            vm.showReBack= true;
            vm.showDistribu=false;

        },
        //分配终端
        distribuTer:function () {
            vm.initQ();
            vm.showReBack= false;
            vm.showDistribu=true;
            vm.showAmount=true;

        },
        reback:function () {
            let flag=vm.checkInfo();
            if(!flag){
                return;
            }

            vm.rebackFun();
        },
        rebackCangku:function(){
                if(vm.q.startNo==null || vm.q.startNo.length==0){
                    layer.msg("请输入开始终端号");
                    return ;
                }
                if(vm.q.endNo==null || vm.q.endNo.length==0){
                    layer.msg("请输入结束终端号");
                    return ;
                }
                if(vm.q.terminalType==0){
                    layer.msg("请选择终端类型");
                    return ;
                }
                confirm("确定要将这些机器回拨到库存么？",function () {
                    vm.q.name=null;
                    vm.q.phone=null;
                    vm.q.agentId=null;
                    vm.rebackFun();
                })
        },
        rebackFun:function(){
            $.ajax({
                url: baseURL + "sys/terminalInfo/rebackTer",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(vm.q),
                success: function (r) {
                    if (r.code == 0) {
                        alert("回拨成功");
                        vm.initQ();
                    } else {
                        alert(r.msg)
                    }
                }
            })

        },
        distribu:function () {
            let flag=vm.checkInfo();
            if(!flag){
                return;
            }
            if(vm.q.isOnline && vm.q.isActiver){
                if(vm.q.amount == null || vm.q.amount.length==0){
                    layer.msg("请输入返现金额");
                    return;
                }
            }

            $.ajax({
                url: baseURL + "sys/terminalInfo/distribuTer",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(vm.q),
                success: function (r) {
                    if (r.code == 0) {
                        alert("分配成功");
                        vm.initQ();
                    } else {
                        alert(r.msg)
                    }
                }
            })

        },
        checkInfo:function(){
            if(vm.q.startNo==null || vm.q.startNo.length==0){
                layer.msg("请输入开始终端号");
                return false;
            }
            if(vm.q.endNo==null || vm.q.endNo.length==0){
                layer.msg("请输入结束终端号");
                return false;
            }
            if(vm.q.terminalType==0){
                layer.msg("请选择终端类型");
                return false;
            }
            if(vm.q.name==null || vm.q.name.length==0){
                layer.msg("请输入结束姓名");
                return false;
            }
            if(vm.q.phone==null || vm.q.phone.length==0){
                layer.msg("请输入结束手机号码");
                return false;
            }
            if(vm.q.agentId==null){
                layer.msg("请输入正确的姓名和手机号码,并查询");
                return false;
            }
            return true;
        },

        checkAgent:function () {
            $.get(baseURL+"sys/sysAgent/checkParent",{'parentName':vm.q.name,'parentPhone':vm.q.phone},result=>{
                console.log(result);
                if(result.code=='0000'){
                    vm.q.agentId=result.parent;
                }else {
                    alert(result.msg)
                }

            })
        },
        isOnline:function () {
            if(vm.q.isActiver){
                vm.q.isOnline=true;
            }

        },bindType:function () {

                $.getJSON(baseURL + "sys/terminalType/list", function (r) {
                    vm.$set(vm.terminalTypes, 0, {
                        terminalType: 0,
                        terminalName: "请选择类型"
                    });
                    for (var i = 0; i < r.page.length; i++) {
                        vm.$set(vm.terminalTypes, (i + 1), r.page[i]);
                    }
                })

        },
        report:function () {
            vm.isReport=true;
            vm.isReback=false;
            let title="线上报备";
            layer.open({
                type:1,
                title:title,
                content:$("#upload"),
                area:['500px','300px'],
                btn:['确定'],
                btn1:function (index) {
                    layer.close(index);
                }
            })
        },
        terminalreback:function(){
            vm.isReport=false;
            vm.isReback=true;
            let title="终端回拨";
            layer.open({
                type:1,
                title:title,
                content:$("#upload"),
                area:['500px','300px'],
                btn:['确定'],
                btn1:function (index) {
                    layer.close(index);
                }
            })
        },
        upload:function (url) {

            var formData = new FormData($("#uploadForm")[0]);
            if ($("#tranfile").val() == "") {
                layer.msg("请选择文件");
                return;
            }
            formData.append("file", $("#file"));

            var index;
            $.ajax({
                url: url,
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                beforeSend: function () {
                    index = layer.load(0)
                },
                complete: function () {
                    layer.close(index);
                },
                success: function (r) {
                    if(r.code==0){

                        layer.msg("新增了" + r.count + "条记录");
                    }else {
                        layer.msg(r.msg);
                    }
                },
                error: function (r) {
                    alert(r.msg);
                }
            });
        },
        reportTer:function () {
            let url =baseURL+"sys/terminalInfo/reportOnline";
            vm.upload(url);
        },
        excelReback:function(){
            let url =baseURL+"sys/terminalInfo/excelReback";
            vm.upload(url);
        },
        downReport:function(){
            window.location.href="http://mpos-img.oss-cn-beijing.aliyuncs.com/templet/%E6%8A%A5%E5%A4%87%E7%BA%BF%E4%B8%8A%E6%A8%A1%E6%9D%BF.xls";
        },
        downReback:function () {
            window.location.href="http://mpos-img.oss-cn-beijing.aliyuncs.com/templet/%E5%9B%9E%E6%8B%A8%E6%9C%BA%E5%85%B7%E6%A8%A1%E6%9D%BF.xls";
        },getDeptId:function () {
            $.get(baseURL + "sys/user/info", function (r) {
                vm.deptId = r.user.deptId;
            })
        },

    }
});

